#include <cstdio>
#include <vector>

using namespace std;

int num;

int teste_inv(vector<int> perm)
{
	int count;
	
	for (int i = 0; i < perm.size(); ++i)
	{
		for (int j = i + 1; j < perm.size(); ++j)
		{
			if (perm[i] > perm[j]) count++;
		}
	}
	
	return count;
}

void permutation(vector<int> v , vector<int> &perm , int k)
{
	for (int i = 0; i < v.size(); ++i)
	{
		vector<int> v2;
		
		for (int j = 0; j < v.size(); ++j)
		{
			v2.push_back(v[j]);
		}
		
		perm.push_back(v2[i]);
		v2.erase(v2.begin() + i);
	
		int aux = teste_inv(perm);
			
		if (aux > k)
		{
			perm.pop_back();	
			return;
		}

		if (v2.size() == 0)
		{
			if (aux == k)
			{
				num++;
			}			
		}
		else
		{
			permutation(v2 , perm , k);
		}
		
		perm.pop_back();
	}
}

int main()
{
	int d;
	
	scanf("%d" , &d);
	
	for (int i = 0; i < d; ++i)
	{
		int n,k;
		
		scanf("%d %d" , &n , &k);
		
		vector<int> v;
		
		for (int i = 1; i <= n; ++i) { v.push_back(i);}
		
		vector<int> perm;
		
		num = 0;
		
		permutation(v , perm , k);
	
		printf("%d\n" , num);
	
	}
	
	
	return 0;
}

